package com.zzq.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.zzq.constant.MessageConstant;
import com.zzq.entity.Result;
import com.zzq.pojo.Ordersetting;
import com.zzq.service.OrdersettingService;
import com.zzq.utils.POIUtils;
import com.zzq.utils.QiniuUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 幼儿猿大班
 * 2021/7/26
 */
@RestController
@RequestMapping("/ordersetting")
public class OrdersettingController {

    @Reference
    private OrdersettingService ordersettingService;
    @RequestMapping("/upload")
    public Result upload(MultipartFile excelFile) throws Exception {

        try {
            List<String[]> strings = POIUtils.readExcel(excelFile);

            ArrayList<Ordersetting> ordersettings = new ArrayList<>();
            for (String[] string : strings) {
                ordersettings.add(new Ordersetting(new Date(string[0]),Integer.parseInt(string[1])));
            }
            ordersettingService.insertBatch(ordersettings);
            return new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }
    }

    @RequestMapping("/getOrderSettingByMonth")
    public Result getOrderSettingByMonth(String date){
        try {
            List<Map<String,Integer>> list=ordersettingService.getOrderSettingByMonth(date);
            return new Result(true,MessageConstant.QUERY_ORDER_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_ORDER_FAIL);
        }
    }

    @RequestMapping("/setNumByDate")
    @PreAuthorize("hasAuthority('ORDERSETTING')")
    public Result setNumByDate(@RequestBody Ordersetting ordersetting){
        System.out.println("请求了");
        try {
            ordersettingService.setNumByDate(ordersetting);
            return new Result(true,MessageConstant.ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ORDERSETTING_FAIL);
        }

    }
}
